package com.qcby.byc.demo0422.controller;

import com.qcby.byc.demo0422.aop.ViewRecords;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

/**
 * @className: LoginController
 * @description:
 * @author: lxt
 * @create: 2021-04-26 20:17
 **/
@Controller
@RequestMapping("login")
public class LoginController {

    @Autowired
    private HttpSession httpSession;

    /**
     *  登录跳转
     */
    @RequestMapping("toLogin")
    public String toLogin(){
        return "login";
    }

    /**
     *  登录
     * @param username
     * @param password
     * @return
     */
    @RequestMapping
    public String login(Model model,String username, String password){
        if(!"admin".equals(username)){
            model.addAttribute("error","用户名错误");
            return "login";
        }
        if(!"66".equals(password)){
            model.addAttribute("error","密码错误");
            return "login";
        }
        httpSession.setAttribute("user","admin");
        return "demo";
    }

    /**
     * 退出登录
     * @return
     */
    @RequestMapping("logout")
    public String logout(){
        httpSession.removeAttribute("user");
        return "login";
    }
}
